package movieGandalf.DB;


import java.io.InputStream;
import java.math.BigInteger;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;

import movieGandalf.dataObjects.Movie;

public class CountriesAdder implements DataReader{
	
	/*public int read(HashMap<String,Object> dest, Encoder enc, Connection conn, InputStream in){
		String line = null;
		PreparedStatement stmt = null;
		Movie movie = null;
		Object o = null;
		
		try {
			boolean isBegin = true;

			while ((line = MovieTools.getLine(in)) != null){
				if (isBegin){
					if ((line.length()<3 || !line.subSequence(0, 3).equals("===")))
						continue;
					else{
						isBegin = false;
						continue;
					}
				}
				
				int stringEnd = line.length();
				int i = 0;
				
				if (stringEnd>=5 && line.startsWith("-----"))
					break;
				
				if (stringEnd==0 || line.charAt(0)=='"')
					continue;
				
				for (i = line.length()-1;i>=0;i--){
					if (line.charAt(i)==')') {
						while (i>=0 && line.charAt(i)!='(')
							i--;
						i--;
						stringEnd=i;
						continue;
					}
					if (line.charAt(i)=='\t'){
						i++;
						break;
					}
				}
				
				if (i<0)
					continue;
				
				if (stringEnd-i<1)
					continue;
				
				String country = line.substring(i, stringEnd);
				
				String imdbName = MovieTools.getIMDBName(line);
				if (imdbName.equals(""))
					continue;
				
				o = dest.get(imdbName);
				
				if (!(o instanceof Movie))
					continue;
				movie = (Movie) o;
				
				movie.setCountry(country);
				
				dest.put(imdbName, movie);
			}
		}
		catch (Exception e){
			e.printStackTrace();
			return 0;
		}
		finally {
			try{
				if (stmt!=null)
					stmt.close();
				in.close();
			}
			catch(Exception e){
				return 0;
			}
		}
		
		return 1;
	}*/
	
	public int read(Connection conn, InputStream in){
		String line = null;
		PreparedStatement stmt = null;
		
		BigInteger movieID = null;

		try {
			
			stmt = conn.prepareStatement("CREATE TEMPORARY TABLE IF NOT EXISTS MovieGandalf.TempCountries (movieID BIGINT UNSIGNED, countryID SMALLINT UNSIGNED, PRIMARY KEY (movieID));");
			stmt.execute();
			stmt.close();
			
			stmt = conn.prepareStatement("REPLACE INTO TempCountries (movieID,countryID) VALUES(?,?)");
			
			boolean isBegin = true;

			while ((line = MovieTools.getLine(in)) != null){
				if (isBegin){
					if ((line.length()<3 || !line.subSequence(0, 3).equals("===")))
						continue;
					else{
						isBegin = false;
						continue;
					}
				}
				
				int stringEnd = line.length();
				int i = 0;
				
				if (stringEnd>=5 && line.startsWith("-----"))
					break;
				
				if (stringEnd==0 || line.charAt(0)=='"')
					continue;
				
				for (i = line.length()-1;i>=0;i--){
					if (line.charAt(i)==')') {
						while (i>=0 && line.charAt(i)!='(')
							i--;
						i--;
						stringEnd=i;
						continue;
					}
					if (line.charAt(i)=='\t'){
						i++;
						break;
					}
				}
				
				if (i<0)
					continue;
				
				if (stringEnd-i<1)
					continue;
				
				String country = line.substring(i, stringEnd);
							
				Integer countryID = MovieTools.getCountryID(conn, country);
				if (countryID == null)
					continue;
				
				String imdbName = MovieTools.getIMDBName(line);
				if (imdbName.equals(""))
					continue;
				
				movieID = MovieTools.getMovieID(conn, imdbName);
				if (movieID == null)
					continue;
				
				
				stmt.setObject(1, movieID);
				stmt.setObject(2, countryID);
				stmt.addBatch();
			}
			
			stmt.executeBatch();
			conn.commit();
			stmt.close();
			
			stmt = conn.prepareStatement("UPDATE Movies INNER JOIN TempCountries ON Movies.movieID=TempCountries.movieID SET Movies.countryID = TempCountries.countryID");
			stmt.addBatch();
			stmt.executeBatch();
			conn.commit();
			stmt.close();
		}
		
		catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
		catch (Exception e){
			e.printStackTrace();
			return 0;
		}
		finally {
			try{
				if (stmt!=null)
					stmt.close();
				in.close();
			}
			catch(Exception e){
				return 0;
			}
		}
		
		return 1;
	}
	
}
